package com.example.nfc.server.utils;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

import java.nio.ByteBuffer;

/**
 * Used to format and apply color to the text that will be printed in the output field.
 */
public final class Output {

    /**
     * Custom green color that will be used to print messages.
     */
    public static final int DARK_GREEN = Color.rgb(100, 200, 100);
    /** SpannanbleStringBuilder that will contain the formatted output text. */
    private static SpannableStringBuilder sOutText = new SpannableStringBuilder();

    /**
     * Sets the color for the input text and makes it uppercase.
     * 
     * @param inputBuffer
     *            (ByteBuffer) - array of hex values
     * @param color
     *            (int) - the color of the string
     * @return (SpannableStringBuilder) - the text with the desired color and formatting
     */
    public static SpannableStringBuilder formatText(final ByteBuffer inputBuffer, final int color) {
        sOutText.clear();
        for (int i = 0; i < inputBuffer.limit(); i++) {
            sOutText.append(String.format("%02X", inputBuffer.get(i)) + " ");
        }
        // if we only receive one hex value then don't append a new line just add a blank space
        if (inputBuffer.limit() > 1) {
            sOutText.append("\n");
        } else {
            sOutText.append(" ");
        }
        sOutText.setSpan(new ForegroundColorSpan(color), 0, sOutText.length(), 0);
        return sOutText;
    }
    public static String toString(final ByteBuffer inputBuffer){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < inputBuffer.limit(); i++) {
            stringBuilder.append(String.format("%02X", inputBuffer.get(i)) + " ");
        }
        return stringBuilder.toString();
    }

    /**
     * Sets the color for the input text.
     * 
     * @param inputText
     *            (String) - the string that will be colored
     * @param color
     *            (int) - the color of the string
     * @return (SpannableStringBuilder) - the text with the desired color set for the span of the
     *         input string
     */
    public static SpannableStringBuilder formatText(final String inputText, final int color) {
        sOutText.clear();
        sOutText.append(inputText);
        sOutText.setSpan(new ForegroundColorSpan(color), 0, sOutText.length(), 0);
        return sOutText;
    }

    /**
     * Sets the color for the input text and makes it uppercase.
     * 
     * @param
     * @param color
     *            (int) - the color of the string
     * @return (SpannableStringBuilder) - the text with the desired color and formatting
     */
    public static SpannableStringBuilder formatText(final byte inputByte, final int color) {
        sOutText.clear();
        sOutText.append(String.format("%02X", inputByte) + " ");
        sOutText.setSpan(new ForegroundColorSpan(color), 0, sOutText.length(), 0);
        return sOutText;
    }

    /**
     * Sets the color for the input text.
     * 
     * @param inputText
     *            (String)
     * @param color
     *            (int)
     */
    private static void printText(final String inputText, final int color) {
        sOutText.clear();
        sOutText.append(inputText + "\n");
        sOutText.setSpan(new ForegroundColorSpan(color), 0, sOutText.length(), 0);
    }

    /** Constructor. Does nothing */
    private Output() {
    }
}
